//
//  WJAutoProgressView.swift
//  WJWeiBo
//
//  Created by wangjun on 16/7/21.
//  Copyright © 2016年 wangjun. All rights reserved.
//

import UIKit

class WJAutoProgressView: UIProgressView {
    
    var isAnimation: Bool = true
    
    var timer: NSTimer?
     override init(frame: CGRect) {
        
        super.init(frame: CGRect(x: 0, y: 64, width: UIScreen.mainScreen().bounds.width, height: 5))
        
        setupUI()
    }

     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
    
    func setupUI() {
        
        self.tintColor = UIColor.greenColor()
//        self.backgroundColor = UIColor.yellowColor()
    }
    
    func removeView() {
        
        self.removeFromSuperview()
    }
    
    func startAnima() {
        
        self.progress = 0;
        
        self.hidden = false
        
       timer = NSTimer.scheduledTimerWithTimeInterval(1/60, target: self, selector: "valueChange", userInfo: nil, repeats: true)
    }
    
    func endAnima() {
        
        isAnimation = false
    }
    
    func valueChange() {
        
        if isAnimation {
            
            self.progress += 0.05
            
            if self.progress >= 0.95 {
                
                self.progress = 0.95
            }
        }else{
            
            if self.progress == 1 {
                
                self.hidden = true
                timer?.invalidate()
                
            }else{
                
                self.progress += 0.05
            }
            
        }
    }
  
    deinit{
        
        timer?.invalidate()
    }

}
